package com.project.livetv.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.project.livetv.R;
import com.project.livetv.adapter.MyAdapterForMainTv;
import com.project.livetv.adapter.MyRecycleForTvTop;
import com.project.livetv.bean.MyMessage;

import java.util.ArrayList;
import java.util.List;

import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.VideoView;


public class MainTVShowActivity extends BaseActivity {
    private VideoView mVideoView;
    private RecyclerView mRecyclerView;
    private ListView mListView;
    private MyAdapterForMainTv myAdapterForMainTv;
    private MyRecycleForTvTop myRecycleForTvTop;
    private RecyclerView.LayoutManager manager;
    private LinearLayout mLinearLayout;
    private RelativeLayout mRelativeLayout;
    private EditText mEditText;
    private String info;

    //视频地址
    private String tvString;

    //测试数据
    private List<MyMessage> mDatas;
    private long time;
    private int ypNum;
    private int roomNum;
    private int livePeos;

    private TextView inkeId, inkeNum, liveNum;



    private ImageView mBackground;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tvshow);


    }

    @Override
    public void initView() {

        mVideoView = (VideoView) findViewById(R.id.vv_mainTv_showLive);
        mRecyclerView = (RecyclerView) findViewById(R.id.rclv_mainTv_show_watcher);
        mListView = (ListView) findViewById(R.id.lv_main_tv_show_infoshow);
        mLinearLayout = (LinearLayout) findViewById(R.id.ll_mainTv_chat);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rlv_mainTv_bottom);
        mEditText = (EditText) findViewById(R.id.et_mainTv_inputInfo);

        inkeId = (TextView) findViewById(R.id.tv_mainTv_ykId);
        inkeNum = (TextView) findViewById(R.id.tv_mainTv_inpiao_num);
        liveNum = (TextView) findViewById(R.id.tv_mainTv_liveNum);


        mBackground = (ImageView) findViewById(R.id.iv_mainTv_backGround);

    }

    @Override
    public void initData() {
        tvString = getIntent().getStringExtra("tvUrl");
        //房间信息
        ypNum = getIntent().getIntExtra("ypNum", 0);
        roomNum = getIntent().getIntExtra("roomNum", 123456);
        livePeos = getIntent().getIntExtra("livePeos", 0);

        mVideoView.setVideoPath(tvString);
        mDatas = new ArrayList<>();

        mVideoView.setVideoLayout(VideoView.VIDEO_LAYOUT_STRETCH, 0);
        mVideoView.requestFocus();
        myAdapterForMainTv = new MyAdapterForMainTv(mDatas, this);
        myRecycleForTvTop = new MyRecycleForTvTop(null, this);
        manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);


    }

    @Override
    public void setData() {
        mListView.setAdapter(myAdapterForMainTv);
        myAdapterForMainTv.notifyDataSetChanged();
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(myRecycleForTvTop);

        inkeId.setText("映客号: " + roomNum);
        inkeNum.setText("" + ypNum);
        liveNum.setText("" + livePeos);

    }

    @Override
    public void setListener() {
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setPlaybackSpeed(1.0f);
                mp.start();
                mBackground.setVisibility(View.GONE);

            }
        });

    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.iv_mainTvShow_infomore:
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    View infoView = LayoutInflater.from(this).inflate(R.layout.person_info_dialog, null);
                    builder.setView(infoView);
                    builder.show();
                    break;
                case R.id.iv_mainTvShow_share_chat:
                    mLinearLayout.setVisibility(View.VISIBLE);
                    mRelativeLayout.setVisibility(View.INVISIBLE);
                    mRelativeLayout.setClickable(false);
                    break;
                case R.id.iv_mainTvShow_message://消息
                    View popView = LayoutInflater.from(this).inflate(R.layout.activity_message, null);

                    TabLayout tableLayout = (TabLayout)popView.findViewById(R.id.tab_layout);
                    ViewPager viewPager = (ViewPager)popView.findViewById(R.id.vp_messageactivity_viewpager);

                    //初始化标签
                    tableLayout.addTab(tableLayout.newTab().setText("好友"));
                    tableLayout.addTab(tableLayout.newTab().setText("关注"));
                    PopupWindow popupWindow = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT,700);
                    popupWindow.setFocusable(true);
                    ColorDrawable dw = new ColorDrawable(Color.parseColor("#55ffffff"));
                    popupWindow.setBackgroundDrawable(dw);
                    // 在底部显示
                    popupWindow.showAtLocation(this.findViewById(R.id.iv_mainTvShow_message),
                            Gravity.BOTTOM, 0, 0);
                    break;
                case R.id.btn_mainTv_send:
                    info = mEditText.getText().toString();
                    if (TextUtils.isEmpty(info)) {
                        Toast.makeText(MainTVShowActivity.this, "信息不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        MyMessage myMessage = new MyMessage("巴布亚", info);
                        mDatas.add(myMessage);
                        myAdapterForMainTv.notifyDataSetChanged();
                        mListView.smoothScrollToPosition(mDatas.size() - 1);
                        mEditText.setText("");
                    }
                    break;
                case R.id.iv_mainTvShow_share:
                    showShare();
                    break;
                case R.id.iv_mainTvShow_gift:
                    //动画

                    View windowView = LayoutInflater.from(this).inflate(R.layout.layout_maintv_gift, null);
                    PopupWindow popup = new PopupWindow(windowView, WindowManager.LayoutParams.MATCH_PARENT,450,true);
                    windowView.setFocusable(true);
                    ColorDrawable dw2 = new ColorDrawable(Color.parseColor("#55666666"));
                    popup.setBackgroundDrawable(dw2);
                    // 在底部显示
                    popup.showAtLocation(this.findViewById(R.id.iv_mainTvShow_gift),
                            Gravity.BOTTOM, 0, 0);

                    ImageView  startImg = (ImageView) windowView.findViewById(R.id.iv_gift_heart);
                    ImageView  startImgFire = (ImageView) windowView.findViewById(R.id.iv_gift_firefox);
                    startImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ImageView animFire = (ImageView) findViewById(R.id.iv_mainTvShow_anim);
                            animFire.setVisibility(View.VISIBLE);
                            animFire.setBackgroundResource(R.drawable.anim_frame_heart);
                            AnimationDrawable drawable = (AnimationDrawable) animFire.getBackground();
                            if(drawable.isRunning()){

                            }else{
                                drawable.start();
                            }
                        }
                    });
                    startImgFire.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ImageView animFire = (ImageView) findViewById(R.id.iv_mainTvShow_anim);
                            animFire.setVisibility(View.VISIBLE);
                            animFire.setBackgroundResource(R.drawable.anim_frame);
                            AnimationDrawable drawable = (AnimationDrawable) animFire.getBackground();
                            if(drawable.isRunning()){

                            }else{
                                drawable.start();
                            }

                        }
                    });
                    break;
                case R.id.iv_mainTv_exit:
                    finish();
                    break;
            }
        }
    }


    //返回键
    @Override
    public void onBackPressed() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_exit, null);
        builder.setView(view);
        builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.setNegativeButton("逗呢", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    //双击隐藏消息框
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (System.currentTimeMillis() - time < 1000) {
                    mLinearLayout.setVisibility(View.GONE);
                    mRelativeLayout.setVisibility(View.VISIBLE);
                }
                time = System.currentTimeMillis();
                break;
        }
        return super.onTouchEvent(event);
    }
}


